package cn.yinuo.website.article.web;

import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.ApiOperation;
import cn.yinuo.website.article.service.IArticleService;
import cn.yinuo.website.article.entity.Article;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.yinuo.website.common.exception.BusinessRuntimeException;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zw
 * @since 2019-05-29
 */
@RestController
@RequestMapping("/api/article/article")
public class ArticleController {
	@Autowired
	private IArticleService articleService;
	@GetMapping
    @ApiOperation("获取数据列表")
	public Page<Article> findListByPage(@RequestParam(name = "page", defaultValue = "1") int pageIndex,@RequestParam(name = "rows", defaultValue = "20") int step){
			Page page = new Page(pageIndex,step);
			Page p = articleService.selectPage(page);
			return p;
	}
	@ApiOperation("根据ID查找数据")
	@GetMapping("/{id}")
	public Article find(@PathVariable("id") Long id){
		Article Article = articleService.selectById(id);
			if(Article==null){
			throw new BusinessRuntimeException("查询不到此id的数据");
			}
			return Article;
	}

	@ApiOperation("添加数据")
	@PostMapping
	public Article addItem(@RequestBody Article Article){
			boolean isOk = articleService.insert(Article);
			if(isOk){
			return Article;
			}
			throw new BusinessRuntimeException("新增失败！");
	}

	@ApiOperation("更新数据")
	@PutMapping
	public Article updateItem(@RequestBody Article Article){
		boolean isOk = articleService.updateAllColumnById(Article);
		if(isOk){
		return Article;
		}
		throw new BusinessRuntimeException("更新失败！");
	}

	@ApiOperation("删除数据")
	@DeleteMapping("{id}")
	public void deleteItem(@PathVariable("id") Long id){
		boolean isOk = articleService.deleteById(id);
		if(!isOk){
		throw new BusinessRuntimeException("数据删除失败");
		}
		}
	}
